package io.github.handyang.algorithms.sort;

public class CountSortAlgorithms implements SortAlgorithms {

    @Override
    public void sort(int[] arr) {
        int max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;

        for (int item : arr) {
            if (item > max) max = item;
            if (item < min) min = item;
        }

        int[] temp = new int[max - min + 1];
        for (int item : arr) {
            temp[item - min]++;
        }

        int index = 0;
        for (int i = 0; i < temp.length; i++) {
            for (int j = 0; j < temp[i]; j++) {
                arr[index++] = min + i;
            }
        }
    }
}
